package com.md.youyouhui.ui.login;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityLoginBinding;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.utils.AddFragmentShow;
import com.md.youyouhui.utils.StatusBarHelper;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.UMShareAPI;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by AdministratorZJ
 * on 2019/4/2.
 *
 * @author AdministratorZJ
 */
public class LoginActivity extends BaseActivity {
    private ActivityLoginBinding loginBinding;
    private AddFragmentShow fragmentShow;

    @Override
    public void setContentLayout() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StatusBarHelper.setFullWindow(getWindow());
            StatusBarHelper.setStatusBarTextColor(getWindow(), true);
        }
        loginBinding = DataBindingUtil.setContentView(this, R.layout.activity_login);
    }

    @Override
    public void initView() {
        fragmentShow = new AddFragmentShow();
        fragmentShow.addFragmentShow(this, R.id.loginLayout, LoginFragment.class);
//

        MobclickAgent.onEvent(this, "Start");//启动

        Map<String, String> map_value = new HashMap<>();
        map_value.put("parentId", "parentId");
        map_value.put("userId", "userId");
        MobclickAgent.onEventValue(this, "Pay_wait", map_value, 100);//等待支付

        // Map<String, String> map_value = new HashMap<String, String>();
        map_value.put("parentId", "parentId");
        map_value.put("userId", "userId");
        MobclickAgent.onEventValue(this, "Pay_over", map_value, 100);//完成支付

        map_value = new HashMap<>();
        map_value.put("parentId", "parentId");
        map_value.put("userId", "userId");
        MobclickAgent.onEventValue(this, "Pay_cancel", map_value, 100);//取消支付
    }

    @Override
    public void initData() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(LoginActivity.this, new String[]{
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                }, 1);
            }

        }
        loginBinding.loginRegister.setOnClickListener(v -> {
            startActivityForResult(new Intent(this, RegisterActivity.class), 101);
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && resultCode == RESULT_OK) {
            finish();
        }
    }

    public void msgLogin() {
        fragmentShow.addFragmentShow(this, R.id.loginLayout, MsgLoginFragment.class);
    }

    public void defaultLogin() {
        fragmentShow.addFragmentShow(this, R.id.loginLayout, LoginFragment.class);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 1) {
            for (int i = 0; i < permissions.length; i++) {
                String permission = permissions[i];
                int grantResult = grantResults[i];
                if (permission.equals(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    if (grantResult == PackageManager.PERMISSION_GRANTED) {
                    } else {
                        Toast.makeText(this, "权限没有打开,会导致程序出错", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    }
}
